<?php
/*
 module:		红娘管理
 create_time:	2022-03-08 11:13:01
 author:		
 contact:		
*/

namespace app\backend\service;


use app\backend\model\Member;
use app\backend\model\ZhAuthentication;
use app\backend\model\ZhMatchmaker;
use app\backend\model\ZhMatchmakerLevel;
use think\exception\ValidateException;
use xhadmin\CommonService;

class ZhMatchmakerService extends CommonService
{


    /*
     * @Description  列表数据
     */
    public static function indexList($where, $field, $order, $limit, $page)
    {
        try {
            $res = ZhMatchmaker::alias('z')->leftJoin('member m', 'm.uid =z.u_id')->where($where)->field($field)->order($order)->paginate(['list_rows' => $limit, 'page' => $page])->toArray();
            for ($i = 0; $i < count($res['data']); $i++) {
                $child_num = Member::where("matchmaker_id", $res['data'][$i]["matchmaker_id"])->count();
                $res['data'][$i]["child_num"]=$child_num;

                //推荐的有效的红娘数
                $matchmaker_num=ZhMatchmaker::where(['top_matchmaker_id'=>$res['data'][$i]["matchmaker_id"],'status'=>1])->count();
                $res['data'][$i]["matchmaker_num"]=$matchmaker_num;

                //有效资料数
                $authentication_num=ZhAuthentication::where(['matchmaker_id'=>$res['data'][$i]["matchmaker_id"],'status'=>1])->count();
                $res['data'][$i]["authentication_num"]=$authentication_num;

                //可体现余额
                $user = Member::where(['uid' => $res['data'][$i]["u_id"]])->find();
                $res['data'][$i]["balance"] = $user['balance']??0;

                //上级推荐人 name
                $top_matchmaker_name                    = ZhMatchmaker::where("matchmaker_id", $res['data'][$i]["top_matchmaker_id"])->column("name");
                $res['data'][$i]['top_matchmaker_name'] = $top_matchmaker_name[0];
                $level_name                             = ZhMatchmakerLevel::where("id", $res['data'][$i]["level_id"])->column("name");
                $res['data'][$i]['level_name']          = $level_name[0];
            }
        } catch (\Exception $e) {
            abort(config('my.error_log_code'), $e->getMessage());
        }
        return ['rows' => $res['data'], 'total' => $res['total']];
    }


    /*
     * @Description  红娘下级列表
     */
    public static function childList($where, $field, $order, $limit, $page)
    {
        try {
            $res = Member::where($where)->field($field)->order($order)->paginate(['list_rows' => $limit, 'page' => $page])->toArray();

        } catch (\Exception $e) {
            abort(config('my.error_log_code'), $e->getMessage());
        }
        return ['rows' => $res['data'], 'total' => $res['total']];
    }

    /*
     * @Description  修改
     */
    public static function update($data)
    {
        try {
            validate(\app\backend\validate\ZhMatchmaker::class)->scene('update')->check($data);
            $res                = ZhMatchmaker::update($data);
        } catch (ValidateException $e) {
            throw new ValidateException ($e->getError());
        } catch (\Exception $e) {
            abort(config('my.error_log_code'), $e->getMessage());
        }
        if (!$res) {
            throw new ValidateException ('操作失败');
        }
        return $res;
    }


}

